#include "uart_process.h"
#include "uart_send.h"
#include "user_cfg.h"

uint8_t uarty_buf[7];

static uart_cfg_t uarty_cfg = {
    uarty_buf,
    sizeof(uarty_buf)
};

//串口初始化
void Send_Init(void)
{
	Uart_Init();
    Uart_Send_Init(UARTY, &uarty_cfg);
}

/**
 * @description: 串口数据打包，间隔50ms轮询发送给主控
 * @param {uint16_t} key_value 触摸按键值
 */
void Uart_Data_Pack(uint16_t key_value)
{
    uint8_t buf[7];
    buf[0] = 0x55;
    buf[1] = 0x00;
    buf[2] = 0x00;
    buf[3] = (uint8_t)(key_value>>8);
    buf[4] = (uint8_t)(key_value);
    buf[5] = (uint8_t)(buf[0] + buf[1] + buf[2] + buf[3] + buf[4]);
    buf[6] = 0xFB;
    Uart_Send(UARTY, buf, sizeof(buf));

}


void Uart_Init(void)
{
    NVIC_InitType NVIC_InitStructure;
	GPIO_InitType GPIO_InitStructure;
	USART_InitType USART_InitStructure;

	// RCC_EnableAPB2PeriphClk(USARTy_GPIO_CLK, ENABLE);
    /* Enable USARTy and USARTz Clock */
    USARTy_APBxClkCmd(USARTy_CLK, ENABLE);


    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = USARTy_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority           = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);

    /* Configure USARTy Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTy_TxPin;
    GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Alternate = USARTy_Tx_GPIO_AF;
    GPIO_InitPeripheral(USARTy_GPIO, &GPIO_InitStructure);
    /* Configure USARTx Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTy_RxPin;
    // GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_INPUT;
    GPIO_InitStructure.GPIO_Alternate = USARTy_Rx_GPIO_AF;
    GPIO_InitPeripheral(USARTy_GPIO, &GPIO_InitStructure);

    /* USARTy and USARTz configuration ------------------------------------------------------*/
    USART_InitStructure.BaudRate            = 9600;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTy and USARTz */
    USART_Init(USARTy, &USART_InitStructure);
	/* Enable USARTy Receive and Transmit interrupts */
    USART_ConfigInt(USARTy, USART_INT_RXDNE, ENABLE);
    USART_ConfigInt(USARTy, USART_INT_TXDE, ENABLE);
	USART_Enable(USARTy, ENABLE);




}
